/*
 * 版权属于onps栈开发团队，遵循Apache License 2.0开源许可协议
 *
 */
#include "port/datatype.h"
#include "port/sys_config.h"
#include "onps_errors.h"
#include "port/os_datatype.h"
#include "port/os_adapter.h"
#include "one_shot_timer.h"
#include "onps_utils.h"
#include "protocols.h"
#include "onps_input.h"
#include "netif/netif.h"

#if NETTOOLS_TELNETSRV
#include "ch32v30x.h"
#include "ch32v30x_eth.h"

#if NETTOOLS_PING && NVTCMD_PING_EN
#include "portmacro.h"
#endif //* #if NETTOOLS_PING && NVTCMD_PING_EN

#if SUPPORT_ETHERNET && NVTCMD_IFIP_EN
//#include "at24c02.h"
#endif //* #if SUPPORT_ETHERNET && NVTCMD_IFIP_EN

#if NETTOOLS_SNTP && NVTCMD_NTP_EN
#include "rtc.h"
#endif //* #if NETTOOLS_SNTP && NVTCMD_NTP_EN

#define SYMBOL_GLOBALS
#include "telnet/os_nvt.h"
#undef SYMBOL_GLOBALS

#define INVALID_TASK_HANDLER NULL

#define THNVT_STK_SIZE 295
static const UCHAR l_ubaTHNvtPrio[NVTNUM_MAX] = {THNVT0_PRIO, THNVT1_PRIO}; 
static struct {
  TaskHandle_t pstcbTaskHandler;
  void *pvParam;
} l_staTHNvt[NVTNUM_MAX];
extern void thread_nvt_handler(void *pvParam); 
void os_nvt_init(void)
{
  UCHAR i;
  for(i=0; i<NVTNUM_MAX; i++)
    l_staTHNvt[i].pstcbTaskHandler = INVALID_TASK_HANDLER;
}

void os_nvt_uninit(void)
{
}

BOOL os_nvt_start(void *pvParam)
{
  UCHAR i; 
  for(i=0; i<NVTNUM_MAX; i++)
  {
    if(INVALID_TASK_HANDLER == l_staTHNvt[i].pstcbTaskHandler)
    {
      l_staTHNvt[i].pvParam = pvParam;
      BaseType_t nRtnVal = xTaskCreate((TaskFunction_t )thread_nvt_handler, "thread_nvt_handler", (USHORT)THNVT_STK_SIZE, pvParam,
                                          (UBaseType_t)l_ubaTHNvtPrio[i], (TaskHandle_t *)&l_staTHNvt[i].pstcbTaskHandler);
      if (pdPASS == nRtnVal)
        return TRUE;

      l_staTHNvt[i].pstcbTaskHandler = INVALID_TASK_HANDLER;

      printf("The creation of task thread_nvt_handler-%d failed, error code: %08X\r\n", i, nRtnVal);
      return FALSE;
    }
  }
  
  return FALSE; 
}

void os_nvt_stop(void *pvParam)
{
  UCHAR i; 
  for(i=0; i<NVTNUM_MAX; i++)
  {
    if(pvParam == l_staTHNvt[i].pvParam && INVALID_TASK_HANDLER != l_staTHNvt[i].pstcbTaskHandler)
    {
      vTaskDelete(l_staTHNvt[i].pstcbTaskHandler);
      l_staTHNvt[i].pstcbTaskHandler = INVALID_TASK_HANDLER;
    }
  }
}

#if SUPPORT_ETHERNET && NVTCMD_IFIP_EN
#if ETH_EXTRA_IP_EN
BOOL os_nvt_add_ip(const CHAR *pszIfName, in_addr_t unIp, in_addr_t unSubnetMask)
{
#warning "os_nvt_add_ip() not yet implemented"
  return TRUE;
}

BOOL os_nvt_del_ip(const CHAR *pszIfName, in_addr_t unIp)
{
#warning "os_nvt_del_ip() not yet implemented"
  return TRUE;
}
#endif //* #if ETH_EXTRA_IP_EN 
BOOL os_nvt_set_ip(const CHAR *pszIfName, in_addr_t unIp, in_addr_t unSubnetMask, in_addr_t unGateway)
{
#warning "os_nvt_set_ip() not yet implemented"
  return TRUE;
}

BOOL os_nvt_set_mac(const CHAR *pszIfName, const CHAR *pszMac)
{
#warning "os_nvt_set_mac() not yet implemented"
  return TRUE;
}

BOOL os_nvt_set_dns(const CHAR *pszIfName, in_addr_t unPrimaryDns, in_addr_t unSecondaryDns)
{
#warning "os_nvt_set_dns() not yet implemented"
  return TRUE;
}

BOOL os_nvt_set_dhcp(const CHAR *pszIfName)
{
#warning "os_nvt_set_dhcp() not yet implemented"
  return TRUE;
}

void os_nvt_system_reset(void)
{
  NVIC_SystemReset(); 
}
#endif //* #if SUPPORT_ETHERNET && NVTCMD_IFIP_EN

#if NVTCMD_ROUTE_EN
BOOL os_nvt_add_route_entry(const CHAR *pszIfName, in_addr_t unDestination, in_addr_t unGenmask, in_addr_t unGateway)
{
#warning "os_nvt_add_route_entry() not yet implemented"
  return TRUE;
}

BOOL os_nvt_del_route_entry(in_addr_t unDestination)
{
#warning "os_nvt_del_route_entry() not yet implemented"
  return TRUE;
}
#endif //* #if NVTCMD_ROUTE_EN

#if NETTOOLS_PING && NVTCMD_PING_EN
UINT os_get_elapsed_millisecs(void)
{
  return HalGetElapsedMSecs();
}
#endif //* #if NETTOOLS_PING && NVTCMD_PING_EN

#if NETTOOLS_SNTP && NVTCMD_NTP_EN
void os_nvt_set_system_time(time_t tNtpTime)
{
  RTCSetSystemUnixTimestamp(tNtpTime); 
}
#endif //* #if NETTOOLS_SNTP && NVTCMD_NTP_EN

#endif //* #if NETTOOLS_TELNETSRV
